class Admins::WeixinsController < ApplicationController
  before_action :set_weixin, only: [:show, :edit, :update, :destroy,:submit,:approve,:reject]

  # GET /weixins
  # GET /weixins.json
  def index
    weixin_type = params[:weixin_type] || '1'
    @current_user = current_user
    @weixins = Weixin.weixin_type(weixin_type,current_user).check(params[:check]).page(params[:page])
  end

  # GET /weixins/1
  # GET /weixins/1.json
  def show
  end

  # GET /weixins/new
  def new
    @weixin = Weixin.new
  end

  # GET /weixins/1/edit
  def edit
  end

  # POST /weixins
  # POST /weixins.json
  def create
    p '==============================================='
    p weixin_params[:content]
    @weixin = Weixin.new(weixin_params.merge({postby: current_user.name}))
    if @weixin.save
      flash[:success] = "微信创建成功"
      redirect_to admins_weixins_url
    else
      flash[:error] = @weixin.errors.full_messages.join(",")
      render :new
    end
  end

  def submit
    respond_to do |format|
      if @weixin.update_attributes(check: 1)
        format.html { redirect_to admins_weixins_path @weixin, notice: '微信送审成功.' }
        format.json { render :show, status: :ok, location: @weixin }
      else
        format.html { render :edit }
        format.json { render json: @weixin.errors, status: :unprocessable_entity }
      end
    end
  end

  #采用
  def approve
    @weixin.update_attributes(check: 2)
    flash[:success] = '微信采用成功.'
    redirect_to admins_weixins_path @weixin
  end

  #不采用
  def reject
    @weixin.update_attributes(check: 3,reason: params[:reason])
    flash[:success] = '微信退回成功.'
    redirect_to admins_weixins_path @weixin
  end

  # PATCH/PUT /weixins/1
  # PATCH/PUT /weixins/1.json
  def update
      if @weixin.update(weixin_params)
        flash[:success] = '微信更新成功'
        redirect_to admins_weixins_url
      else
        render :edit
      end
  end

  # DELETE /weixins/1
  # DELETE /weixins/1.json
  def destroy
    @weixin.destroy
    flash[:success] = "微信删除成功"
    redirect_to admins_weixins_url
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_weixin
      @weixin = Weixin.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def weixin_params
      params.require(:weixin).permit(:author, :contribute_unit_name, :phone, :content,:title)
    end
end
